
package com.mti.monitor.controller;


import com.github.pagehelper.PageInfo;
import com.mti.monitor.bean.PageableIn;
import com.mti.monitor.bean.model.MonitorLogParam;
import com.mti.monitor.bean.model.MonitorPage;
import com.mti.monitor.bean.model.SysMonitorConfig;
import com.mti.monitor.bean.req.MonitorReq;
import com.mti.monitor.enums.TaskTypeEnum;
import com.mti.monitor.rst.Result;
import com.mti.monitor.service.DataMonitorService;
import com.mti.monitor.service.ServerMonitorService;
import com.mti.monitor.service.StatisticsService;
import com.mti.monitor.service.SysMonitorConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "monitor", description = "monitor")
@RestController
@RequestMapping("/monitor")
@Slf4j
public class MonitorController {

    @Autowired
    private SysMonitorConfigService sysMonitorConfigService;

    @Autowired
    private ServerMonitorService serverMonitorService;

    @Autowired
    private DataMonitorService dataMonitorService;

    @Autowired
    private StatisticsService statisticsService;


    @ApiOperation(value = "查看监控列表", notes = "查看监控列表")
    @PostMapping(value = "/list")
    public PageInfo<SysMonitorConfig> list(@RequestBody PageableIn req) {
        return sysMonitorConfigService.selectAllList(req);
    }

    @ApiOperation(value = "查看配置", notes = "查看配置", httpMethod = "GET")
    @GetMapping(value = "/selectById/{id}")
    public SysMonitorConfig selectById(@PathVariable("id") String id) {
        return sysMonitorConfigService.selectById(id);
    }

    @ApiOperation(value = "查看监控列表", notes = "查看监控列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public PageInfo<SysMonitorConfig> page(@RequestBody MonitorReq req) {
        return sysMonitorConfigService.selectMonitorConfigPage(req);
    }

    @ApiOperation(value = "查看监控日志列表", notes = "查看监控日志列表", httpMethod = "POST")
    @PostMapping(value = "/logPage")
    public MonitorPage logPage(@RequestBody MonitorLogParam page) {
        if (page == null) {
            page.setSize(10);
            page.setStart(1);
        }
        return sysMonitorConfigService.selectMonitorLogPage(page);
    }

    @ApiOperation(value = "新增监控配置", notes = "新增监控配置", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody SysMonitorConfig config) {
        sysMonitorConfigService.createConfig(config);
        return Result.ok();
    }

    @ApiOperation(value = "更新监控配置", notes = "更新监控配置", httpMethod = "POST")
    @PostMapping(value = "/update")
    public Result<Object> update(@RequestBody SysMonitorConfig config) {
        sysMonitorConfigService.updateConfig(config);
        return Result.ok();
    }

    @ApiOperation(value = "保存监控配置", notes = "保存监控配置", httpMethod = "POST")
    @PostMapping(value = "/save")
    public Result<Object> addOrUpdate(@RequestBody SysMonitorConfig config) {
        if (config.getId() == null || StringUtils.isEmpty(config.getId())) {
            sysMonitorConfigService.createConfig(config);
        } else {
            sysMonitorConfigService.updateConfig(config);
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除监控配置", notes = "删除监控配置", httpMethod = "GET")
    @GetMapping(value = "/deleteConfig/{id}")
    public Result<Object> deleteConfig(@PathVariable("id") String id) {
        sysMonitorConfigService.deleteMonitor(id);
        return Result.ok();
    }

    @ApiOperation(value = "启动监控", notes = "启动监控", httpMethod = "GET")
    @GetMapping(value = "/start/{id}")
    public Result<Object> start(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        sysMonitorConfigService.startMonitor(ids);
        return Result.ok();
    }

    @ApiOperation(value = "停止监控", notes = "停止监控", httpMethod = "GET")
    @GetMapping(value = "/stop/{id}")
    public Result<Object> stop(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        sysMonitorConfigService.stopMonitor(ids);
        return Result.ok();
    }

    @ApiOperation(value = "立即执行", notes = "立即执行", httpMethod = "GET")
    @GetMapping(value = "/runNow/{id}")
    public Result<Object> runNow(@PathVariable("id") String id) {
        SysMonitorConfig config = sysMonitorConfigService.selectOneById(id);
        if (config.getTaskType().equals(TaskTypeEnum.SERVER.getType())) {
            serverMonitorService.monitorServerHeart(config.getId());
        } else if (config.getTaskType().equals(TaskTypeEnum.DATA.getType())) {
            dataMonitorService.monitorData(config.getId());
        } else if (config.getTaskType().equals(TaskTypeEnum.STATISTICS.getType())) {
            statisticsService.statisticsData(config.getId());
        } else {
            log.warn("{}监控任务{}监控类型{}不存在", config.getId(), config.getTaskName(), config.getTaskType());
        }
        return Result.ok();
    }

    @ApiOperation(value = "手动重启服务", notes = "手动重启服务", httpMethod = "GET")
    @GetMapping(value = "/restartServer/{id}")
    public Result<Object> restartServer(@PathVariable("id") String id) {
        serverMonitorService.restartServer(id);
        return Result.ok();
    }
}
